package com.zhang;

import com.zhang.config.WebMvcConfig;
import com.zhang.filter.LoginFilter;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;

/**
 * 主启动类,服务器tomcat
 */
public class Boot {


    public Boot(){
        //创建tomcat服务器对象
        Tomcat tomcat = new Tomcat();
        //设置服务器的端口号,浏览器访问的端口号
        tomcat.setPort(8080);
        tomcat.getConnector();

        //创建web容器上下文
        Context context = tomcat.addContext("", null);

//        //创建过滤器对象
//        FilterDef def = new FilterDef();g
//        def.setFilter(new LoginFilter());
//        def.setFilterName("LoginFilter");
//        //
//        FilterMap map = new FilterMap();
//        map.setFilterName("LoginFilter");
//        map.addURLPattern("/html/addMain.html/*");
//        map.addURLPattern("/reply/add/*");
//
//        context.addFilterDef(def);
//        context.addFilterMap(map);
        //注册过滤器
        registerFilter(context,new LoginFilter(),"/html/addMain.html","/html/reply.html");

        //创建应用上下文
        WebApplicationContext applicationContext = createApplicationContext(context.getServletContext());

        DispatcherServlet servlet = new DispatcherServlet(applicationContext);
        Wrapper wrapper = Tomcat.addServlet(context, "", servlet);
        wrapper.setLoadOnStartup(0);
        wrapper.addMapping("/*");

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建springMvc的应用上下文
     * @param servletContext 服务器上下文
     * @return springMvc的应用上下文
     */
    public static WebApplicationContext createApplicationContext(
            ServletContext servletContext) {
        AnnotationConfigWebApplicationContext ctx
                = new AnnotationConfigWebApplicationContext();
        ctx.register(WebMvcConfig.class);

        ctx.setServletContext(servletContext);
        ctx.refresh();
        ctx.registerShutdownHook();
        return ctx;
    }

    public static void main(String[] args) {
        new Boot();
    }

    /**
     * 在web容器上下文中注册过滤器
     * @param context web上下文
     * @param filter 过滤器
     * @param url 过滤的范围（就是那些请求被过滤）
     */
    private void registerFilter(Context context, Filter filter,String... url){
        FilterDef def = new FilterDef();
        def.setFilter(filter);
        def.setFilterName(filter.getClass().getName());

        FilterMap map = new FilterMap();
        map.setFilterName(filter.getClass().getName());
        for (int i = 0; i < url.length; i++) {
            map.addURLPattern(url[i]);
        }

        context.addFilterDef(def);
        context.addFilterMap(map);
    }
}
